<%@page import="java.util.*,hyper.market.beans.*,java.io.*"%>
<%@page contentType="text/html"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%-- 
    Document   : viewStores
    Created on : Aug 25, 2011, 2:17:10 PM
    Author     : Mikeno
--%>

<jsp:useBean id="storeList" class="java.util.ArrayList" scope="session"/>
<jsp:useBean id="notilist" class="java.util.ArrayList" scope="session"/>
<link rel="stylesheet" href="style/desk.css" type="text/css" />
<link rel="stylesheet" href="TinyAccordion/style.css"/>
<link rel="stylesheet" href="style/960.css" type="text/css" />
<link rel="stylesheet" href="style/960_12_col.css" type="text/css" />
<link rel="stylesheet" href="style/reset.css" type="text/css" />

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome To HyperMarket Admin Page</title>
        <script src="scripts/jquery-1.6.4.min.js"></script>

        <script type = "text/javascript">
            var modalWindow = {
                parent:"body",
                windowId:null,
                content:null,
                width:null,
                height:null,
                close:function()
                {
                    $(".modal-window").remove();
                    $(".modal-overlay").remove();
                },
                open:function()
                {
                    var modal = "";
                    modal += "<div class=\"modal-overlay\"></div>";
                    modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
                    modal += this.content;
                    modal += "</div>";

                    $(this.parent).append(modal);

                    $(".modal-window").append("<a class=\"close-window\"></a>");
                    $(".close-window").click(function(){modalWindow.close();});
                    $(".modal-overlay").click(function(){modalWindow.close();});
                }
            };
            var openMyModal = function(source)
            {
                modalWindow.windowId = "myModal";
                modalWindow.width = 500;
                modalWindow.height = 340;
                modalWindow.content = "<iframe width='500' height='340' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'></iframe>";
                modalWindow.open();
            };
            
            function verifyAction(selectIndex,id, storename,nid){
                if(document.getElementById("Vimg"+selectIndex).style.display == 'block'){
                    var r=confirm("You wish to overwrite your previous action?");
                    if (r==true)
                    {
                        commitNotification(selectIndex,id, storename,nid);
                    }
                    else
                    {
                        return false;
                    }

                }else{
                    var r=confirm("Confirm Action?");
                    if (r==true)
                    {
                        commitNotification(selectIndex,id, storename,nid);
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            function initRequest() {
                if (window.XMLHttpRequest) {
                    return new XMLHttpRequest();
                } else if (window.ActiveXObject) {
                    isIE = true;
                    return new ActiveXObject("Microsoft.XMLHTTP");
                }
            }

            function commitNotification(selectIndex,id, storename,nid) {

                var action = document.getElementById("actions"+ selectIndex).value;
                var url = "/HyperMarket/NotificationActionServlet?actionvalue=" +action +"&stockid="+id+"&storename="+storename+"&nid="+nid;
                var req = initRequest();
                req.onreadystatechange = function() {
                    if (req.readyState == 4) {
                        if (req.status == 200) {
                            parseMessages(req.responseXML,selectIndex);
                        } else if (req.status == 204){
                            alert('Theres something wrong with the connection, try again =)');
                        }
                    }
                };
                req.open("POST", url, true);
                req.send(null);
            }


            function parseMessages(responseXML,k) {
                var commitAction = responseXML.getElementsByTagName("valid")[0];
                var success =  commitAction.childNodes[0].nodeValue;
                if(success){
                    commitedActions(k);
                    alert('Done!');
                }else{
                    alert('Theres something wrong with the connection, try again =)');
                }

            }

            function commitReject(selectIndex,id, storename,nid) {

                var action = document.getElementById("actions"+ selectIndex).value;
                var url = "/HyperMarket/NotificationActionServlet?actionvalue=" +action +"&stockid="+id+"&storename="+storename+"&nid="+nid;
                var req = initRequest();
                req.onreadystatechange = function() {
                    if (req.readyState == 4) {
                        if (req.status == 200) {
                            parseRejectMessages(req.responseXML,selectIndex);
                        } else if (req.status == 204){
                            alert('Theres something wrong with the connection, try again =)');
                        }
                    }
                };
                req.open("POST", url, true);
                req.send(null);
            }


            function parseRejectMessages(responseXML,k) {
                var commitAction = responseXML.getElementsByTagName("valid")[0];
                var success =  commitAction.childNodes[0].nodeValue;
                if(success){
                    commitedActions(k);
                    alert('Done!');
                }else{
                    alert('Theres something wrong with the connection, try again =)');
                }

            }


            function commitedActions(k){
                document.getElementById("Vimg"+k).style.display = 'block';
            }
        </script>
    </head>
    <body>
        <div class="header group">
            <div class="pagewidth">

                <ul class="headernav">
                    <li><a href="/HyperMarket/ViewStoresServlet"><img class="headerlogo" src="./arts/logo.png" width="106" height="22" alt="Bagcheck Home" border="0" /></a></li>
                    <li class="headerlink "><a p='header' t='home' href="/HyperMarket/ViewStoresServlet">Home</a></li>
                    <li class="headerlink headerlinkhl"><a p='header' t='about' href="#" onclick="return false;">View All Stores</a></li>
                    <li class="headerlink "><a p='header'  href="/HyperMarket/createStockServlet">Create Stocks</a></li>
                    <li class="headerlink "><a p='header'  href="/HyperMarket/EndOfDayServlet">End Of Day</a></li>
                    <li class="headerlink "><a p='header'  href="/HyperMarket/LogoutServlet">Logout</a></li>

                </ul>
            </div>
        </div>
        <div class="container_12">
            <div id="notifList" class="grid_6">
               
                    <%
                                String storename = "";
                                int rowcounter = 1;
                             if(notilist.size() != 0){

                                 out.println("<label class='notificationLabel'>Notification</label>");
                                 out.println("<ul class='acc' id='acc'>");
                                for (int i = 0; i < notilist.size(); i++) {
                                    if (!((NotificationBean) notilist.get(i)).getStoreName().equals(storename)) { // if current is not equal to new
                                        // print out a new list
                                        if (i != 0) {
                                            out.println("</table></div></div></li>");
                                        }
                                        // print new list
                                        rowcounter = 1;
                                        storename = ((NotificationBean) notilist.get(i)).getStoreName();
                                        out.println("<li><h3>" + storename + "</h3><div class=\"acc-section\"> <div class=\"acc-content\"><table id='notiTable'>");  // create a new nest of list;
                                    }

                                    out.println("<tr><td cellspacing='10'>" + rowcounter + "</td>");
                                    rowcounter++;
                                    // creating ONE node
                                    out.println("<td>" + ((NotificationBean) notilist.get(i)).getDesc() + "</td>");
                                    out.println("<td cellspacing='10' >" + ((NotificationBean) notilist.get(i)).getStaffName() + "</td>");
                                    out.println("<td cellspacing='10'>" + ((NotificationBean) notilist.get(i)).getCreatedDate() + "</td>");
                                    // 1 - order more, discontinue , OUT of Stock / Low in Stock (view) (Order More) <dropdown> (discontinue)
                                    // 2 - edit item , requesting more discount (View) (Link)
                                    // 3 - acknowledge of expiredGoods (View) ( Okay)
                                    switch (((NotificationBean) notilist.get(i)).getAction()) {
                                        case 1:
                                            out.println("<td><select id=\"actions" + i + "\">");
                                            out.println("<option value='1'>Order More</option>");
                                            out.println("<option value='2'>Discontinue</option>");
                                            out.println("</select></td>");
                                            out.println("<td><input type='button' value='Commit' onclick=\"verifyAction(" + i + "," + ((NotificationBean) notilist.get(i)).getStockdetailid() + ",'" + storename + "','" + ((NotificationBean) notilist.get(i)).getNotificationid() + "')\"></td>");
                                            out.println("<td><img id=\"Vimg" + i + "\" style='display:none;' src='./images/verify_icon.gif'></td></tr>");
                                            break;
                                        case 2:
                                            out.println("<td><a href=\"/HyperMarket/NotificationAdjustServlet?stockid=" + ((NotificationBean) notilist.get(i)).getStockdetailid() + "&nid=" + ((NotificationBean) notilist.get(i)).getNotificationid() + "&storename=" + storename + "&action=1\" target=\"_blank\" onclick=\"commitedActions(" + i + ");openMyModal('/HyperMarket/NotificationAdjustServlet?stockid=" + ((NotificationBean) notilist.get(i)).getStockdetailid() + "&nid=" + ((NotificationBean) notilist.get(i)).getNotificationid() + "&storename=" + storename + "&action=1');return false;\" >Adjust Price</a></td>");
                                            out.println("<td><a href=\"/HyperMarket/NotificationCancelServlet?stockid=" + ((NotificationBean) notilist.get(i)).getStockdetailid() + "&nid=" + ((NotificationBean) notilist.get(i)).getNotificationid() + "&storename=" + storename + "&createBy=" + ((NotificationBean) notilist.get(i)).getStaffName() + "\" target=\"_blank\" onclick=\"commitedActions(" + i + ");openMyModal('/HyperMarket/NotificationCancelServlet?stockid=" + ((NotificationBean) notilist.get(i)).getStockdetailid() + "&nid=" + ((NotificationBean) notilist.get(i)).getNotificationid() + "&storename=" + storename + "&createBy=" + ((NotificationBean) notilist.get(i)).getStaffName() + "');return false;\">Cancel</a></td>");
                                            out.println("<td><img id=\"Vimg" + i + "\" style='display:none;' src='./images/verify_icon.gif'></td></tr>");
                                            break;
                                        case 3:
                                            break;
                                    }

                                }
                                }else{
                                    out.println("<label class='notificationLabel'>No Notification</label>");
                                    }

                    %>
                </table></div></div></li>
</ul>


</div>
<br>
<br>
<div class="grid_6">
    <table border ="1" class="" id="storelist">
        <tr>
            <td>
                <img src="images/storename.png">
            </td>
            <td>
               <img src="images/storeAdd.png">
            </td>
            <td>
               <img src="images/storeno.png">
            </td>
        </tr>
        <%
                    for (int count = 0; count < storeList.size(); count++) {
                        if(count%2 == 0){
                             out.println("<tr class ='storelist_tr_odd'>");
                        }else{
                             out.println("<tr class ='storelist_tr_even'>");
                        }
                        out.println("<td class = 'storelist_left'>");
                        out.println("<a href=\"/HyperMarket/viewStoreDetailsServlet?value=" + ((StoreBean) storeList.get(count)).getStoreid() + "\" > " + ((StoreBean) storeList.get(count)).getStoreName() + "</a>");
                        out.println("</td> <td>");
                        out.println("<p>" + ((StoreBean) storeList.get(count)).getAddress() + "</p>");
                        out.println("</td> <td class = 'storelist_right'>");
                        out.println("<p>" + ((StoreBean) storeList.get(count)).getTelephone() + "</p>");
                        out.println("</td> </tr>");
                    }
        %>

    </table>
</div>
</div>
<script type="text/javascript" src="./TinyAccordion/script.js"></script>
<script type="text/javascript">

    var parentAccordion=new TINY.accordion.slider("parentAccordion");
    parentAccordion.init("acc","h3",-1,-1);

    var nestedAccordion=new TINY.accordion.slider("nestedAccordion");
    nestedAccordion.init("nested","h3",1,-1,"acc-selected");

</script>
</body>
</html>
